/*
 * Copyright (C) 2011, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include "VCNetChatServer.h"
#include "VCModNet.h"

VCModNetServer::VCModNetServer(VCIController & controller, VCNetChatServerClient & client)
: VCIMod(controller)
, m_client(client)
, m_recv_buff(VCNETCHATSERVERCLIENT_BUFF_SIZE)
{
}

VCModNetServer::~VCModNetServer()
{
}

VC_RESULT VCModNetServer::Push(VC_CMD cmd, void * data_buff, size_t data_size, VC_DATAFORMAT data_format)
{
	VC_RESULT result = VC_OK;

	VCAutoMutex lock(m_client_mutex);
	MODNET_HEAD head;
	head.cmd = (long)cmd;
	head.data_size = (unsigned long)data_size;
	head.data_format = (long)data_format;
	m_client.Send(&head, sizeof(MODNET_HEAD));
	m_client.Send(data_buff, data_size);

	return result;
}

void VCModNetServer::RecvFromNet(void * data, size_t data_size)
{
	VCAutoMutex lock(m_recv_buff_mutex);
	m_recv_buff.Push(data, data_size);
	size_t recv_buff_size = m_recv_buff.Size();
	if (recv_buff_size >= sizeof(MODNET_HEAD))
	{
		MODNET_HEAD * head = (MODNET_HEAD *)m_recv_buff.GetDataPtr();
		size_t data_size = (size_t)head->data_size;
		if (recv_buff_size >= (sizeof(MODNET_HEAD) + data_size))
		{
			char * data = (char *)m_recv_buff.GetDataPtr() + sizeof(MODNET_HEAD);
			if (m_reciver)
			{
				m_reciver->Push((VC_CMD)head->cmd, (void *)data, data_size, (VC_DATAFORMAT)head->data_format);
			}
			m_recv_buff.PopSize(sizeof(MODNET_HEAD) + data_size);
		}
	}
}

VCNetChatServerClient::VCNetChatServerClient(VC_SOCKET socket, VC_SOCKET_ADDR_IN * addr, VCTCPServerThread & server_thread)
: VCTCPServerClient(socket, addr, server_thread)
, VCIController()
, m_mixernode(NULL)
, m_netchatnode(NULL)
, m_encnode(NULL)
, m_decnode(NULL)
{
	VCIController & controller = dynamic_cast<VCIController &>(*this);
	VCNetChatServer & server = dynamic_cast<VCNetChatServer &>(GetServer());
	
	m_netchatnode = new VCModNetServer(controller, *this);
	m_mixernode = server.m_mixer.NewNode(controller);
	m_encnode = new VCModSpeexEnc(controller);
	m_decnode = new VCModSpeexDec(controller);

	m_decnode->SetReceiver(m_mixernode);
	m_mixernode->SetReceiver(m_encnode);
	m_encnode->SetReceiver(m_netchatnode);
	m_netchatnode->SetReceiver(m_decnode); // should be the last
}

VCNetChatServerClient::~VCNetChatServerClient()
{
	m_netchatnode->SetReceiver(NULL);
	m_mixernode->SetReceiver(NULL);
	m_encnode->SetReceiver(NULL);
	m_decnode->SetReceiver(NULL);

	VCNetChatServer & server = dynamic_cast<VCNetChatServer &>(GetServer());
	server.m_mixer.DeleteNode(m_mixernode);
	delete m_netchatnode;
}

void VCNetChatServerClient::OnRecv(void * data, size_t data_size)
{
	if (m_netchatnode && m_netchatnode->GetReceiver())
	{
		if (m_recv_buff.Size())
		{
			m_netchatnode->RecvFromNet(m_recv_buff.GetDataPtr(), m_recv_buff.Size());
			m_recv_buff.Clear();
		}
		m_netchatnode->RecvFromNet(data, data_size);
	} else
	{
		m_recv_buff.Push(data, data_size);
	}
}

void VCNetChatServerClient::OnDisconect()
{
}

VCNetChatServer::VCNetChatServer()
: VCTCPServer()
{
}

VCNetChatServer::~VCNetChatServer()
{
}

VCTCPServerClient * VCNetChatServer::OnConnect(VC_SOCKET socket, VC_SOCKET_ADDR_IN * addr, VCTCPServerThread & server_thread)
{
	return new VCNetChatServerClient(socket, addr, server_thread);
}

